package com.duowei.diandan.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.databinding.ChoicePayFragmentBinding;

public class ChoicePayFragment extends Fragment {
    public static final String TAG = "ChoicePayFragment";

    private ChoicePayFragmentBinding mBinding;

    /** 微信支付、支付宝支付、会员付款 */
    public static final int TYPE_FOUR = 0;
    /** 非会员付款、会员付款 */
    public static final int TYPE_NUMBERS = 1;
    /** 微信支付、支付宝支付\前台提交 */
    public static final int TYPE_QR_CODE = 2;

    private int mType;

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnButtonClickListener != null) {
                int id = v.getId();

                if (id == R.id.backButton) {
                    mOnButtonClickListener.onBackButtonClick();

                } else if (id == R.id.weixinCardView) {
                    mOnButtonClickListener.onWeixinClick();

                } else if (id == R.id.alipayCardView) {
                    mOnButtonClickListener.onAlipayClick();

                } else if (id == R.id.noCouldCardView) {
                    mOnButtonClickListener.onNoCloudClick();

                } else if (id == R.id.couldCardView) {
                    mOnButtonClickListener.onCloudClick();

                }
            }
        }
    };

    public static ChoicePayFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);

        ChoicePayFragment fragment = new ChoicePayFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mType = getArguments().getInt("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_choice_pay, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        handleFuc();

        mBinding.backButton.setOnClickListener(mOnClickListener);

        mBinding.weixinCardView.setOnClickListener(mOnClickListener);
        mBinding.alipayCardView.setOnClickListener(mOnClickListener);
        mBinding.noCouldCardView.setOnClickListener(mOnClickListener);
        mBinding.couldCardView.setOnClickListener(mOnClickListener);
    }

    private void handleFuc() {
        if (mType == TYPE_FOUR) {
            mBinding.weixinCardView.setVisibility(View.VISIBLE);
            mBinding.alipayCardView.setVisibility(View.VISIBLE);
            mBinding.noCouldCardView.setVisibility(View.GONE);

            if (Settings.CLOUD_FUC) {
                mBinding.couldCardView.setVisibility(View.VISIBLE);
            } else {
                mBinding.couldCardView.setVisibility(View.GONE);
            }

        } else if (mType == TYPE_NUMBERS) {
            mBinding.weixinCardView.setVisibility(View.GONE);
            mBinding.alipayCardView.setVisibility(View.GONE);
            mBinding.noCouldCardView.setVisibility(View.VISIBLE);
            mBinding.couldCardView.setVisibility(View.VISIBLE);

        } else if (mType == TYPE_QR_CODE) {
            mBinding.weixinCardView.setVisibility(View.VISIBLE);
            mBinding.alipayCardView.setVisibility(View.VISIBLE);
            mBinding.noCouldCardView.setVisibility(View.GONE);
            mBinding.couldCardView.setVisibility(View.GONE);
        }
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        mOnButtonClickListener = onButtonClickListener;
    }

    private OnButtonClickListener mOnButtonClickListener;

    public interface OnButtonClickListener {
        void onBackButtonClick();

        void onWeixinClick();

        void onAlipayClick();

        void onNoCloudClick();

        void onCloudClick();
    }
}
